*--------------------------------------------------------------; * Separate ratio estimate of population mean and total, and ; * individual estimates of R, for a stratified random sample ; * with auxiliary variable X ; *--------------------------------------------------------------; %macro s_ratio(sample=,response=,strata=,param=,setup=, npop=,n=,mu_x=,tau_x=,x=,fpc=,wts=); %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&strata) = 0 %then %let strata = %str(strata); %if %length(&setup) = 0 %then %let setup = %str(setup); %if %length(&x) = 0 %then %let x = %str(x); %if %length(&tau_x) > 0 %then %let mu_x = %str(&tau_x/&npop); %else %put %str(*** Neither MU_X nor TAU_X was defined ***); proc sort data = &setup; by &strata; %if %length(&npop) > 0 %then %do; proc means data = &setup noprint; var &npop; output out = ntot_ sum = ntotal_; data ntot_; set ntot_; call symput('ntot',trim(left(ntotal_))); run; %end; data est_; set &sample( keep = &x &response &strata); xy_ = &x*&response; proc sort data = est_; by &strata; proc means data = est_ noprint; by &strata; var &x &response xy_; output out = sep_out_ sum = sumxi_ sumyi_ sumxyi_ css = ssxxi_ ssyyi_ n = n1_ n2_ ni_ mean = xbar ybar; data sep_out_; merge sep_out_ &setup; by &strata; ssxyi_ = sumxyi_ - sumxi_*sumyi_/ni_; ri_ = sumyi_/sumxi_; mu_hati_ = ri_*(&mu_x); sri_ = (ssyyi_ + ri_**2*ssxxi_ - 2*ri_*ssxyi_)/(ni_-1); %if %length(&npop) > 0 %then %do; fpc_ = (&npop-ni_)/&npop; %end; %else %do; fpc_ = 1; %end; %if %length(&fpc) > 0 %then %do; fpc_ = 1; %end; %if %length(&npop) = 0 %then %do; wts_ = &wts; %end; %else %do; wts_ = &npop/&ntot; %end; m_hati_ = wts_*mu_hati_; vterm_ = wts_**2*fpc_*sri_/ni_; drop sumyi_ sumxi_ sumxyi_ ssxxi_ ssyyi_ ssxyi_; proc means data = sep_out_ noprint; var m_hati_ vterm_; output out = s_rato_ sum = mu_hat_ vnumer_; data est_sr_; set s_rato_; var_mu_ = vnumer_; std_mu_ = sqrt(var_mu_); bnd_mu_ = 2*std_mu_; %if %index(%upcase(¶m),TOTAL) > 0 %then %do; tau_hat_ = (&ntot)*mu_hat_; std_tau_ = (&ntot)*std_mu_; bnd_tau_ = 2*std_tau_; %end; proc print data = sep_out_ noobs split='*'; title1 "Ratio Estimates by Strata"; title2 "Response Variable = &response"; title3 "(Auxiliary Variable = &x)"; label &strata = "Strata"; label ni_ = "Sample*Size"; label ri_ = "r(i)"; var &strata ni_ ri_; %if %index(%upcase(¶m),MEAN) > 0 %then %do; proc print data = est_sr_ noobs split='*'; title1 "Separate Ratio Estimate of Mean"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response"; title4 "(Auxiliary Variable = &x)"; label mu_hat_ = 'Estimate'; label std_mu_ = 'Standard*Error'; label bnd_mu_ = 'Bound'; var mu_hat_ std_mu_ bnd_mu_; %end; %if %index(%upcase(¶m),TOTAL) > 0 %then %do; proc print data = est_sr_ noobs split='*'; title1 "Separate Ratio Estimate of Total"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response"; title4 "(Auxiliary Variable = &x)"; label tau_hat_ = 'Estimate'; label std_tau_ = 'Standard*Error'; label bnd_tau_ = 'Bound'; var tau_hat_ std_tau_ bnd_tau_; %end; run; title; %mend s_ratio;